<template>
    <orh-table add="add" pages :pageDate="pageDate"
        @on-add="addSilde" @on-page-change="queryData"
        del="del" @on-del="confirm"
        reload  @on-reload="refresh">
            <Table :columns="columns1" @on-selection-change="selectOne" :loading="tableload" :data="datalist" slot="table"></Table>             
            <orh-sidebar v-model="show" :title="pageTitle" scale slot="others">
                <component :is='which_to_show' :infoId='infoId' @showParent='showParent=>show=showParent' @backList='backList'></component>
            </orh-sidebar>
    </orh-table>
</template>
<script>
export default {
    components: {
        RightsAllocationAdd: resolve => {require(['./RightsAllocationAdd.vue'], resolve);},
        RightsAllocationEdit: resolve => {require(['./RightsAllocationEdit.vue'], resolve);},
    },
    name: "teacher-rightsallocation",
    data () {
        return {
            listUrl:'api/teacher/v1/attendancedey/list?page=',
            delUrl:'api/teacher/v1/attendancedey/del/',

            pageDate: {
                total: 0,
                current_page: 1,
                per_page: 10
            },
            pageTitle:null,//右边页面的title
            tableload:true,//表格加载动画
            
            
            del:[],//删除
            show:false,
            which_to_show: "" ,//判断修改或添加组件显示与否
            datalist:[],//年级变量
            infoId: null,//用于向修改页面传id值
            columns1: [                
                {
                    type: 'selection',
                    width: 60,
                    align: 'center',
                    fixed:'left'
                },
                {
                    title: '姓名',
                    render: (h, params) => {
                        let name = params.row.tescher_name.teacher_name.zh?params.row.tescher_name.teacher_name.zh:params.row.tescher_name.teacher_name.mn
                        return h('div', name);
                    }
                },                 
                {
                    title: '部门',
                    render: (h, params) => {
                        if(params.row.department_name !=null){
                             let name = params.row.department_name.department_name.zh?params.row.department_name.department_name.zh:params.row.department_name.department_name.mn
                             return h('div', name);
                        }
                       
                    }
                },             
                {
                    title: '所分岗位',
                    render: (h, params) => {
                        if(params.row.position_name !=null){
                            let name = params.row.position_name.position_name.zh?params.row.position_name.position_name.zh:params.row.position_name.position_name.mn
                            return h('div', name);
                        }                       
                    }
                },
                {
                    title: '操作',
                    key: 'action',
                    width: 150,
                    align: 'center',
                    render: (h, params) => {
                        return h('div', [
                            h('Button', {
                                directives: [
                                    {
                                    name: 'button',
                                    value: 'edit',
                                    }
                                ],
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px',
                                },
                                on: {
                                    click: () => {
                                        this.update(params.row.id)
                                    }
                                }
                            }, '编辑'),
                            h('Button', {
                                directives: [
                                    {
                                    name: 'button',
                                    value: 'del',
                                    }
                                ],
                                props: {
                                    type: 'error',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px',
                                },
                                on: {
                                    click: () => {
                                        this.del = [params.row.id]
                                        this.confirm()
                                    }
                                }
                            }, '删除'),
                        ]);
                    }
                }
            ],  
        }
    },       
    methods: {    
        /************************************************************
         * 
         * 请求列表数据
         * 
         ************************************************************/
        queryData(index=1){
            this.tableload = true 
            this.$http.get(`${this.listUrl}${index}`).then(val=>{    
                this.datalist=val.data 
                if(val.pages){
                    this.pageDate.total=val.pages.total
                    this.pageDate.per_page=val.pages.per_page
                    this.pageDate.current_page=val.pages.current_page
                } 
                this.tableload = false                  
            }).catch(err=>{
                if(err.code && err.code==422) {
                    this.$Message.error(err.message);
                }
                this.tableload = false                  
            });
        },         
        
        /************************************************************
         * 
         * 添加
         * 
         ************************************************************/
        addSilde () {
            this.show=true
            this.pageTitle='添加'
            this.which_to_show='RightsAllocationAdd';
        },
        
        /************************************************************
         * 
         * 编辑
         * 
         ************************************************************/
        update(index){  
            this.infoId = index
            this.show=true
            this.pageTitle='修改'
            this.which_to_show='RightsAllocationEdit';
        },

        /************************************************************
         * 
         * 修改添加返回数据更新
         * 
         ************************************************************/
        backList(val){            
            if(val == true){
                this.queryData(this.pageDate.current_page);
            }
        },        
        
        /************************************************************
         * 
         * 批量删除
         * 
         ************************************************************/
        selectOne(index){
            this.del = index.map(item=> item.id)
        },

        /************************************************************
         * 
         * 执行删除操作
         * 
         ************************************************************/
        confirm () {
            this.$Modal.confirm({
                title: '数据删除',
                content: '<p>您确定要删除这些数据吗?</p>',
                onOk: () => {
                    if(this.del.length==0){
                        this.$Message.error('请选择删除项');
                        return
                    }
                    this.del = JSON.stringify(this.del)
                    this.tableload = true 
                    this.$http.delete(`${this.delUrl}${this.del}`)
                    .then(val=>{  
                        this.del=[]
                        this.queryData(this.pageDate.current_page)    
                        this.$Message.success(val.message);           
                    })
                    .catch(err=>{
                        if(err.code && err.code==422) {
                            this.$Message.error(err.message);
                        }
                        this.del=[]
                        this.tableload = false 
                    })
                },
            });
        },


        /************************************************************
         * 
         * 刷新数据（当前分页）
         * 
         ************************************************************/
        refresh(){
            this.queryData(this.pageDate.current_page)
        },
    },
    mounted(){
        this.queryData();
    },
}
</script>